<?php
namespace Admin\Controller;

use Think\Controller;
class ToolController extends BaseController {

    // 获取下拉框
    public function region(){
        $parent_id =I('parent_id');
        $result = M('region')->where(array('parent_id'=> $parent_id))->field('id,name')->select();
        $this->ajaxSuccess($result);
    }

    //获取商品分类
    public function category()
    {
        //获取父级ID
        $parent = I("post.parent");

        //获取分类列表
        $children = M("Category")
            ->where(array("parent_id"=>$parent))
            ->select();

        $this->ajaxReturn($children);
    }

    /**
     * 获取网络接口里面的文件流
     **/
    public function GetWebFileStream($strUrl,$urlParams = '',$type = 'get'){
        $stream = "";
        if(!isset($strUrl) || empty($strUrl))
            return "";


        //初始化
        $ch = curl_init();
        if($type === 'post'){
            curl_setopt_array($ch,array(
                CURLOPT_URL              => $strUrl,
                CURLOPT_RETURNTRANSFER  => 1,
                CURLOPT_POST             => 1,
                CURLOPT_HEADER           => 0,
                CURLOPT_POSTFIELDS      => $urlParams)
            );
        }
        else{
            curl_setopt_array($ch,array(
                CURLOPT_URL              => $strUrl,
                CURLOPT_RETURNTRANSFER  => 1,
                CURLOPT_HEADER           => 0)
            );
        }


        //输出结果
        $stream = curl_exec($ch);


        //判断curl请求是否超时
        if(curl_errno($ch)){
            $stream = file_get_contents($strUrl);
        }


        //关闭
        curl_close($ch);

        return $stream;
    }

    // 上传图片
    public function uploadImage()
    {
        if (IS_AJAX)
        {
            //获取待上传文件
            $file = current($_FILES);

            //上传文件
            $result = upload_file($file);
            if ($result)
            {
                $this->ajaxReturn(array("status"=>0,"url"=>$result["url"]));
            }
            else
            {
                $this->ajaxReturn(array("error"=>"图片上传失败！"));
            }
        }
    }

    // 删除图片
    public function deleteImage()
    {
        if (IS_AJAX) {
            $this->ajaxReturn(array("status" => 0));
        }
    }

    /**
     * 富文本编辑框
     */
    public function ueditor()
    {
        $adminRoot = C("ADMIN_ROOT");
        $config = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents($adminRoot . "config.json")), true);
        $action = $_GET['action'];

        switch ($action) {
            case 'config':
                $result =  json_encode($config);
                break;

            /* 上传图片 */
            case 'uploadimage':
                /* 上传涂鸦 */
            case 'uploadscrawl':
                /* 上传视频 */
            case 'uploadvideo':
                /* 上传文件 */
            case 'uploadfile':
                $result = $this->uploadUEditor($config);
                break;

            /* 列出图片 */
            case 'listimage':
                $result = include("action_list.php");
                break;
            /* 列出文件 */
            case 'listfile':
                $result = include("action_list.php");
                break;

            /* 抓取远程文件 */
            case 'catchimage':
                $result = include("action_crawler.php");
                break;

            default:
                $result = json_encode(array(
                    'state'=> '请求地址出错'
                ));
                break;
        }

        /* 输出结果 */
        if (isset($_GET["callback"])) {
            if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
                echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
            } else {
                echo json_encode(array(
                    'state'=> 'callback参数不合法'
                ));
            }
        } else {
            echo $result;
        }
    }

    /**
     * 上传UEditor的资源文件
     *
     * @param $config
     * @return string
     */
    private function uploadUEditor($config)
    {
        //通过配置判断要加载的文件名
        $fieldName = "";
        switch (htmlspecialchars($_GET['action'])) {
            case 'uploadimage':
                $fieldName = $config['imageFieldName'];
                break;
            case 'uploadscrawl':
                $fieldName = $config['scrawlFieldName'];
                break;
            case 'uploadvideo':
                $fieldName = $config['videoFieldName'];
                break;
            case 'uploadfile':
            default:
                $fieldName = $config['fileFieldName'];
                break;
        }

        //执行文件上传
        $file = $_FILES[$fieldName];
        $result = upload_file($_FILES[$fieldName]);

        if ($result)
        {
            $result = array(
                "state" => "SUCCESS",
                "url" => $result["url"],
                "title" => $result["savename"],
                "original" => $file["name"],
                "type" => $result["type"],
                "size" => $result["size"]
            );
            return json_encode($result);
        }
    }
}